﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.IO;
using COFE.Core.Validater;
using System.Threading.Tasks;
using System.Threading;
using COFE.Core.Utils;

namespace COFE.Core.Tasks
{
    public enum ActionParameters { None, One, Fixed, All }

    public class ActionAttribute : PropertyTypeAttribute, IActionFilter
    {        
        #region Constructor

        public ActionAttribute(ActionParameters actionInput, ActionParameters actionOutput)
            //ParentGuid, CurrentActionId, CancelToken, Input, Param, Output.
            : base(typeof(Func<IEntryList, string, IEntryList>))
        {
            _actionInput = actionInput;
            _actionOutput = actionOutput;              
        }

        public ActionAttribute(int numberOfInput, ActionParameters actionOutput)
            : this(ActionParameters.Fixed, actionOutput)
        {            
            _numberOfInput = numberOfInput;
        }

        #endregion

        #region Methods

        public IEnumerable<IFileSystemInfo> Filter(IEnumerable<IFileSystemInfo> input)
        {
            int count = 0;
            var etor = input.GetEnumerator();
            while (etor.MoveNext())
            {
                yield return etor.Current;
                count++;
                switch (_actionInput)
                {
                    case ActionParameters.All :
                        //Do nothing
                        break;
                    case ActionParameters.One :
                        yield break;
                    case ActionParameters.Fixed :
                        if (count >= _numberOfInput)
                            yield break;
                        break;
                }                    
            }
        }

        #endregion

        #region Data
        
        private int _numberOfInput = 0; //Only work if actionInput == Fixed.
        private ActionParameters _actionInput, _actionOutput;

        #endregion

        #region Public Properties

        #endregion


      

        
    }



    public static class ActionAttributeExtension
    {
        public static ActionAttribute GetActionAttribute(object action)
        {
            return EnumUtils<ActionAttribute>.FindAttribute(action);
        }
    }
}
